package com.zzuli.ylsystem.controller;

import com.alibaba.fastjson.JSONObject;
import com.zzuli.ylsystem.dao.*;
import com.zzuli.ylsystem.entity.*;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "主页接口")
@RestController
@RequestMapping("/api/home")
@CrossOrigin
public class HomeController {

    @Value("${ylsystem.path.upload}")
    private String uploadPath;

    @Autowired
    private ImagesDao imagesDao;

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private IconlistDao iconlistDao;

    @Autowired
    private MiddleDao middleDao;

    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private HospitalDao hospitalDao;

    @Autowired
    private FabuDao fabuDao;

    @GetMapping("/images")
    public List<JSONObject> getHeader() {
        List<JSONObject> result = new ArrayList<>();
        List<Map> resultMap = new ArrayList<>();
        List<Images> list = imagesDao.selectAll();
        for (int i=0;i<list.size();i++){
            Map<String, Object> imageMap = new HashMap<>();
            imageMap.put("id",list.get(i).getId());
            imageMap.put("image",list.get(i).getImage());
            imageMap.put("link",list.get(i).getLink());
            resultMap.add(imageMap);
        }

        for (int j=0;j<resultMap.size();j++){
            JSONObject jsonObj = new JSONObject(resultMap.get(j));
            result.add(jsonObj);
        }
        return result;
    }

    @GetMapping("/imageText")
    public Result getImText() {
        List<JSONObject> result = new ArrayList<>();
        List<Map> resultMap = new ArrayList<>();
        List<Comment> list = commentDao.selectAll();
        for (int i=0;i<list.size();i++){
            Map<String, Object> imageMap = new HashMap<>();
            imageMap.put("id",list.get(i).getId());
            imageMap.put("image",list.get(i).getImage());
            imageMap.put("text", list.get(i).getText());
            resultMap.add(imageMap);
        }
        for (int j=0;j<resultMap.size();j++){
            JSONObject jsonObj = new JSONObject(resultMap.get(j));
            result.add(jsonObj);
        }
        System.out.println(result);
        return Result.ok(result);
    }

    @GetMapping("/iconList")
    public Result iconList() {
        List<JSONObject> result = new ArrayList<>();
        List<Map> resultMap = new ArrayList<>();
        List<Iconlist> list = iconlistDao.selectAll();
        for (int i=0;i<list.size();i++){
            Map<String, Object> imageMap = new HashMap<>();
            imageMap.put("id",list.get(i).getId());
            imageMap.put("name",list.get(i).getName());
            imageMap.put("icon", list.get(i).getIcon());
            resultMap.add(imageMap);
        }
        for (int j=0;j<resultMap.size();j++){
            JSONObject jsonObj = new JSONObject(resultMap.get(j));
            result.add(jsonObj);
        }
        System.out.println(result);
        return Result.ok(result);
    }

    @GetMapping("/middleList")
    public Result middleList() {
        List<Middle> list = middleDao.selectDept();

        Middle middle0 = list.get(0);
        Map<String,Object> param0 = new HashMap<>();
        List<Middle> sign1List = middleDao.selectSign1();
        param0.put("product_list",sign1List);
        middle0.setParam(param0);

        Middle middle1 = list.get(1);
        Map<String,Object> param1 = new HashMap<>();
        List<Middle> sign2List = middleDao.selectSign2();
        param1.put("product_list",sign2List);
        middle1.setParam(param1);

        Middle middle2 = list.get(2);
        Map<String,Object> param2 = new HashMap<>();
        List<Middle> sign3List = middleDao.selectSign3();
        param2.put("product_list",sign3List);
        middle2.setParam(param2);

        return Result.ok(list);
    }

    @GetMapping("/selectAllHospOrArticleOrFaBu/{id}")
    public Result selectAllHospOrArticle(@PathVariable Integer id) {
        if (id==1){
            List<Hospital> list = hospitalDao.selectAllHosp();
            return Result.ok(list);
        }else if (id==2){
            List<Article> list = articleDao.selectAllArticle();
            return Result.ok(list);
        }else {
            List<Fabu> list = fabuDao.selectAllFabu();
            return Result.ok(list);
        }
    }

//    @GetMapping("/selectAllArticle")
//    public Result selectAllArticle() {
//        List<Article> list = articleDao.selectAllArticle();
//        return Result.ok(list);
//    }

}
